﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Smg.Source;
using Smg.DAL;
using Smg.BLL;
using Smg.Model;

namespace Smg
{
    public partial class frmNewSqlTemplate : Form
    {
        public frmNewSqlTemplate()
        {
            InitializeComponent();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (this.txtSql.Text.Equals("") || this.txtUrlExpression.Text.Equals(""))
            {
                MessageBox.Show("请将信息填写完整！", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtSql.Focus();
                return;
            }

            Secrecy se = new Secrecy();
            string strPreview = se.UrlTransferred(Config.Url + this.txtUrlExpression.Text.Trim()).Replace("&", "&&");
            try
            {
                DataSet ds = MsSqlOp.GetPrewer(this.txtSql.Text.Trim());
                if (StaticUtil.IsDsHasData(ds))
                {
                    int intColumnsCount = ds.Tables[0].Columns.Count - 1;
                    string strExpression = this.txtUrlExpression.Text.Trim();

                    if (intColumnsCount != StaticUtil.GetCharCountInString(strExpression,"{"))
                    {
                        MessageBox.Show("Sql语句和Url表达式参数个数不匹配！", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    string[] agrs = new string[ds.Tables[0].Columns.Count - 1];

                    for (int i = 0; i < ds.Tables[0].Columns.Count-1; i++)
                    {
                        agrs[i] = ds.Tables[0].Rows[0][i].ToString();
                    }
                    strPreview = string.Format(strPreview, agrs);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.lblPreview.Text = strPreview;
                this.lblPreview.Visible = true;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Config.Sql = this.txtSql.Text.Trim();
            Config.UrlExpression = Config.Url + this.txtUrlExpression.Text.Trim();
            Config.Priority = this.txtPriority.Text.Trim();
            Config.ChangeFreq = this.cbbChangeFreq.Text;

            this.Close();
        }

        private void frmNewSqlTemplate_Load(object sender, EventArgs e)
        {
            this.txtSql.Text = Config.Sql;
            this.cbbChangeFreq.SelectedIndex = 6;
            if (Config.UrlExpression.Length > 0)
            {
                this.txtUrlExpression.Text = Config.UrlExpression.Substring(Config.Url.Length, Config.UrlExpression.Length - Config.Url.Length);
            }
            this.txtPriority.Text = Config.Priority;
        }
    }
}